package com.sunrise.xdoc.entity.order;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

import org.hibernate.annotations.Cascade;

import com.sunrise.xdoc.entity.AbstractCodeEntity;
import com.sunrise.xdoc.entity.config.Ban;

@Entity
@Table(name = "order_don_hang")
@Inheritance(strategy = InheritanceType.JOINED)
public class DonHang extends AbstractCodeEntity {
	private Set<Ban> bans = new HashSet<>();
	private List<ChiTietDonHang> chiTietDonHangs = new ArrayList<>();
	private TrangThaiDonHang trangThai = TrangThaiDonHang.DAT_HANG;
	private Date thoiGianDatHang;
	private Date thoiGianPhucVu;
	private Date thoiGianThanhToan;
	private String maNhanVienPhucVu;
	private long tongGiaSanPham = 0L;
	private long phiDichVu = 0L;
	private int giamGia = 0; // tính theo %
	private long tongGiaThanhToan = 0L;
	private String ghiChu;

	/*
	 * Neu thoiGianDatHang vuot qua 15 phut, hien canh bao den nhan vien de phuc
	 * vu kip thoi
	 */
	public enum TrangThaiDonHang {
		DAT_HANG, DA_PHUC_VU, DA_THANH_TOAN
	}

	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "at_order_ban", joinColumns = { @JoinColumn(name = "order_id") }, inverseJoinColumns = { @JoinColumn(name = "ban_id") })
	public Set<Ban> getBans() {
		return bans;
	}

	public void setBans(Set<Ban> bans) {
		this.bans = bans;
	}

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "donHang", orphanRemoval = true)
	@Cascade(value = { org.hibernate.annotations.CascadeType.ALL })
	public List<ChiTietDonHang> getChiTietDonHangs() {
		return chiTietDonHangs;
	}

	public void setChiTietDonHangs(List<ChiTietDonHang> chiTietDonHangs) {
		this.chiTietDonHangs = chiTietDonHangs;
	}

	@Column(name = "trang_thai")
	@NotNull
	@Enumerated(EnumType.STRING)
	public TrangThaiDonHang getTrangThai() {
		return trangThai;
	}

	public void setTrangThai(TrangThaiDonHang trangThai) {
		this.trangThai = trangThai;
	}

	@Column(name = "thoi_gian_dat_hang")
	public Date getThoiGianDatHang() {
		return thoiGianDatHang;
	}

	public void setThoiGianDatHang(Date thoiGianDatHang) {
		this.thoiGianDatHang = thoiGianDatHang;
	}

	@Column(name = "thoi_gian_phuc_vu")
	public Date getThoiGianPhucVu() {
		return thoiGianPhucVu;
	}

	public void setThoiGianPhucVu(Date thoiGianPhucVu) {
		this.thoiGianPhucVu = thoiGianPhucVu;
	}

	@Column(name = "thoi_gian_thanh_toan")
	public Date getThoiGianThanhToan() {
		return thoiGianThanhToan;
	}

	public void setThoiGianThanhToan(Date thoiGianThanhToan) {
		this.thoiGianThanhToan = thoiGianThanhToan;
	}

	@Column(name = "ma_nhan_vien")
	public String getMaNhanVienPhucVu() {
		return maNhanVienPhucVu;
	}

	public void setMaNhanVienPhucVu(String maNhanVienPhucVu) {
		this.maNhanVienPhucVu = maNhanVienPhucVu;
	}

	@Column(name = "tong_gia_san_pham")
	public long getTongGiaSanPham() {
		return tongGiaSanPham;
	}

	public void setTongGiaSanPham(long tongGiaSanPham) {
		this.tongGiaSanPham = tongGiaSanPham;
		updateGiaThanhToan();
	}

	@Column(name = "phi_dich_vu")
	public long getPhiDichVu() {
		return phiDichVu;
	}

	public void setPhiDichVu(long phiDichVu) {
		this.phiDichVu = phiDichVu;
		updateGiaThanhToan();
	}

	@Column(name = "giam_gia")
	@Min(value = 0)
	@Max(value = 100)
	public int getGiamGia() {
		return giamGia;
	}

	public void setGiamGia(int giamGia) {
		this.giamGia = giamGia;
		updateGiaThanhToan();
	}

	private void updateGiaThanhToan() {
		this.tongGiaThanhToan = (long) (this.tongGiaSanPham * (1 - Double.valueOf(this.giamGia) / 100) + this
				.getPhiDichVu());
	}

	@Column(name = "tong_gia_thanh_toan")
	public long getTongGiaThanhToan() {
		return tongGiaThanhToan;
	}

	public void setTongGiaThanhToan(long tongGiaThanhToan) {
		this.tongGiaThanhToan = tongGiaThanhToan;
	}

	@Column(name = "ghi_chu")
	public String getGhiChu() {
		return ghiChu;
	}

	public void setGhiChu(String ghiChu) {
		this.ghiChu = ghiChu;
	}

}
